package com.Set集合.TreeSet案例;

public class Student implements Comparable<Student>{
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student s) {
//        return 0;//添加一个元素
//        return 1;//按照升序存储元素
//        return 0;//按照降序存储元素
        int i = this.age - s.age;//按照年龄排序
        int sum = i == 0 ? this.name.compareTo(s.name):i;//如果年龄一样就按照姓名排序
        return sum;
    }
}
